<?php

require_once('tools.php');

class Database{

    private $server = DB_SERVER_;
    private $username = DB_USERNAME_;
    private $password = DB_PASSWORD_;
    private $databaseName = 'debatepapo';
    private $connection;
	private $result;

    public function  __construct(){
        $this->connection = mysql_connect($this->server, $this->username, $this->password);
        mysql_select_db($this->databaseName, $this->connection);

        mysql_query("SET NAMES 'utf8'"); //to correct charcode
        mysql_query('SET character_set_connection=utf8');
        mysql_query('SET character_set_client=utf8');
        mysql_query('SET character_set_results=utf8');
    }

    public function execute($query){
        $this->result = mysql_query($query) or die (mysql_error());
        return $this->result;
    }

	public function getNumberOfRows(){
		return mysql_num_rows($this->result);
	}
	
	public function toArray($mode = MYSQL_ASSOC){
		$result = $this->result;
		$array = array();
        
		while($row = mysql_fetch_array($result, $mode)) array_push($array, $row);

		return $array;
	}

    public function getInsertedId(){
        return mysql_insert_id();
    }

    public function getResult(){
        return $this->result;
    }
	
    public function getAffectedRows(){
        return mysql_affected_rows($this->connection);
    }

    public function truncate($table){
        $this->execute('TRUNCATE TABLE '.$table);
    }

    public function getInsertQuery($table, $params){
        $fields = array();
        $values = array();

        foreach($params as $field => $value){
            $value = $this->resolveValue($value);

            $fields[] = $field;
            $values[] = $value;
        }

        $fields = implode(',',$fields);
        $values = implode(',',$values);
    
        return "INSERT INTO $table ($fields) VALUES ($values)";
    }

    public function getUpdateQuery($table, $params, $condition){
        $conditionValues = array();
        $fieldValues = array();

        foreach($params as $field => $value){
            $value = $this->resolveValue($value);

            $fieldValues[] = "$field=$value";
        }

        foreach($condition as $field => $value){
            $value = $this->resolveValue($value);

            $conditionValues[] = "$field=$value";
        }

        $conditionValues = implode(' AND ', $conditionValues);
        $fieldValues = implode(',', $fieldValues);
        
        return "UPDATE $table SET $fieldValues WHERE $conditionValues"; 
    }   

    public function getDeleteQuery($table, $condition){
        $conditions = array();

        foreach($condition as $field => $value){
            $value = $this->resolveValue($value);
            $conditions[] = $field.'='.$value;
        }

        $conditions = implode(' AND ',$conditions);
        
        return "DELETE FROM $table WHERE $conditions";
    }

    public function resolveValue($value){
        if(is_array($value)){
            $type = $value[1];
            $value = $value[0];
        }else $type = false;

        if($value === null){
            $value = 'NULL';
        }else if($type){
            if($type == 'string') $value = "'$value'";
        }else{
            if(!is_numeric($value)) $value = "'$value'";
        }

        return $value;
    }

}

function assocArrayToNumArray($array){
    $numArray = array();
            
    foreach($array as $k => $v) $numArray[] = $v;

    return $numArray;
}

?>
